/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.dao.impl;

import com.legendshop.dao.impl.GenericDaoImpl;
import com.legendshop.dao.support.CriteriaQuery;
import com.legendshop.dao.support.EntityCriterion;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.oa.constants.MailCategoryEnum;
import com.legendshop.oa.dao.MailEntityDao;
import com.legendshop.oa.model.MailEntity;
import com.legendshop.util.AppUtils;

import java.util.List;
/**
 * The Class MailEntityDaoImpl.
 */

public class MailEntityDaoImpl extends GenericDaoImpl<MailEntity, Long> implements MailEntityDao {
     
    public List<MailEntity> getMailEntity(String userName){
    	return this.queryByProperties(new EntityCriterion().eq("userName", userName));
    }

	public MailEntity getMailEntity(Long id){
		return getById(id);
	}
	
    public void deleteMailEntity(MailEntity mailEntity){
    	delete(mailEntity);
    }
	
	public Long saveMailEntity(MailEntity mailEntity){
		return (Long)save(mailEntity);
	}
	
	public void updateMailEntity(MailEntity mailEntity){
		 update(mailEntity);
	}
	
	public PageSupport getMailEntity(CriteriaQuery cq){
		return queryPage(cq);
	}
	
	/**
	 * 查询发送的邮件
	 * @param receiveMail
	 * @param type
	 * @return
	 */
	@Override
	public MailEntity getMailEntity(String receiveMail, MailCategoryEnum type) {
		List<MailEntity> result =  this.queryLimit("SELECT ls_mail.* FROM  ls_mail LEFT JOIN ls_mail_log ON ls_mail.id = ls_mail_log.mail_id " +
				"WHERE ls_mail.status=1 and ls_mail_log.receive_mail= ? AND ls_mail.category=? ORDER BY ls_mail.rec_time DESC  ", MailEntity.class, 0,1, receiveMail, type.value());
		if(AppUtils.isBlank(result)){
			return null;
		}else{
			return result.get(0);
		}
	}

	@Override
	public void clearMailCode(Long id) {
		this.update("update ls_mail set status=0 where id=?",id);
	}

	@Override
	public PageSupport<MailEntity> getMailEntityPage(String curPageNO) {
		CriteriaQuery cq = new CriteriaQuery(MailEntity.class, curPageNO);
        //cq.setPageSize(PropertiesUtil.getObject(SysParameterEnum.PAGE_SIZE, Integer.class));
        //cq = hasAllDataFunction(cq, request, StringUtils.trim(mailEntity.getUserName()));
		return queryPage(cq);
	}

	
 }
